<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="师傅名称" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入师傅名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['clothing:clothing:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['clothing:clothing:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['clothing:clothing:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['clothing:clothing:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="clothingList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" >
        <template #default="scope">
          <!-- 小于12位默认展示 -->
          <div v-if="scope.row.title != null && scope.row.title.length <= 10">
            {{ scope.row.title }}
          </div>
          <!-- 大于12为只展示12位 -->
          <el-tooltip
            v-if="scope.row.title != null && scope.row.title.length > 10"
            popper-class="testtooltip"
            :content="scope.row.title"
            placement="top-start"
          >
            <div>{{ scope.row.title.slice(0, 10) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="images" >
         <template #default="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="案例说明" align="center" prop="memo" >
         <template #default="scope">
          <!-- 小于12位默认展示 -->
          <div v-if="scope.row.memo != null && scope.row.memo.length <= 10">
            {{ scope.row.memo }}
          </div>
          <!-- 大于12为只展示12位 -->
          <el-tooltip
            v-if="scope.row.memo != null && scope.row.memo.length > 10"
            popper-class="testtooltip"
            :content="scope.row.memo"
            placement="top-start"
          >
            <div>{{ scope.row.memo.slice(0, 10) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="师傅名称" align="center" prop="masterName" />
      <el-table-column label="是否推荐首页" align="center" prop="isCommend" >
         <template #default="scope">
          {{scope.row.isCommend==0?'否':'是'}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Expand"
            @click="checkInfoChange(scope.row)"
            v-hasPermi="['clothing:index:info']"
            >查看详情</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['clothing:index:edit']"
            >是否首页推荐</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['clothing:index:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服装案例对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="clothingRef"
        :model="form"
        :rules="rules"
        :disabled="true"
        label-width="80px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="详情" prop="intro">
          <el-input type="textarea" :rows="10" v-model="form.intro" placeholder="请输入详情" />
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <!-- <el-upload
            class="avatar-uploader"
            action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload> -->
           <image-upload v-model="form.images" readOnly="false"/>
          <!-- <el-input v-model="form.images" placeholder="请输入图片" /> -->
        </el-form-item>
      </el-form>
      <!-- <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template> -->
    </el-dialog>
    <!-- <el-dialog
      style="border-radius: 8px"
      v-model="dialogTableVisible"
      title="订单详情"
      width="800"
    >
      <div class="grid-data">
        <div class="grid-data-content">
          <div class="grid-data-list">
            <div class="grid-header-text">
              <span>订单编号</span>
              <span>{{ gridDataContentDesk?.orderNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>订单标题</span>
              <span>{{ gridDataContentDesk?.title }}</span>
            </div>
            <div class="grid-header-text">
              <span>面料图片</span>
              <img :src="gridDataContentDesk?.images" alt="" />
            </div>
            <div class="grid-header-text">
              <span>购买单号</span>
              <span>{{ gridDataContentDesk?.orderNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>物流编号</span>
              <span>{{ gridDataContentDesk?.orderNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>快递公司</span>
              <span>{{ gridDataContentDesk?.orderNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>接单时间</span>
              <span>{{ gridDataContentDesk?.orderNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>师傅姓名</span>
              <span>{{ gridDataContentDesk?.orderNo }}</span>
            </div>
            <div class="grid-header-text">
              <span>总价</span>
              <span>{{ gridDataContentDesk?.totalPrice }}</span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog> -->
  </div>
</template>

<script setup name="Clothing">
import {
  listClothing,
  getClothing,
  delClothing,
  addClothing,
  updateClothing,
} from "@/api/main/clothing";
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance();
const route = useRoute();
const clothingList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dialogTableVisible = ref(false);
const gridDataContentDesk = ref();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    intro: null,
    images: null,
    memo: null,
    status: null,
    createUser: null,
    createName: null,
    updateUser: null,
    updateName: null,
  },
  rules: {
    status: [{ required: true, message: "状态不能为空", trigger: "change" }],
    delFlag: [
      { required: true, message: "是否删除;0否1是不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询服装案例列表 */
function getList(value) {
  // 检查params.id是否存在且不为空
    if (route.params.id) {
        queryParams.value.couponId = value.id;
    }
  loading.value = true;
  listClothing(queryParams.value).then((response) => {
    clothingList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    intro: null,
    images: null,
    memo: null,
    status: null,
    delFlag: null,
    createUser: null,
    createName: null,
    createTime: null,
    updateUser: null,
    updateName: null,
    updateTime: null,
  };
  proxy.resetForm("clothingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList(route.params);
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加服装案例";
}

/** 修改按钮操作 */
function handleUpdate(row) {
   proxy.$modal
    .confirm('是否确认推荐服装案例编号为"' + row.id + '"的数据项？')
    .then(function () {
      return updateClothing({id: row.id,isCommend:1});
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["clothingRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateClothing(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClothing(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除服装案例编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delClothing(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "clothing/clothing/export",
    {
      ...queryParams.value,
    },
    `clothing_${new Date().getTime()}.xlsx`
  );
}

getList(route.params);

const handleAvatarSuccess = (e) => {
  console.log(e);
};

// 查看详情
const checkInfoChange = (parms) => {
  open.value = true;
  getClothing(parms.id).then(async (res) => {
    form.value = res.data;
  });
};
</script>
<style lang="scss" scoped>
.grid-data-list {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  .grid-header-text {
    height: 60px;
    width: 50%;
    display: flex;
    align-items: center;
    border: 1px solid #eee;
    img {
      width: 50px;
      height: 50px;
    }
    &:nth-child(3n) {
      background: #fafafa;
    }
    span {
      &:nth-child(1) {
        width: 25% !important;
        display: block;
        padding-left: 20px;
      }
    }
  }
}
</style>
